package com.zh.note.huawei.onehundred.敏感字段加密;

/**
 * @Author: zhanghuan
 * @date: 2023/9/3 19:59
 * @description: create it
 * <p>
 * 1
 * password__a12345678_timeout_100
 * <p>
 * 2
 * aaa_password_"a12_45678"_timeout__100_""_
 */

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        try {
            Scanner scan = new Scanner(System.in);
            // int k = Integer.parseInt(scan.nextLine());
            // String s = scan.nextLine();
            int k = 2;
            String s = "aaa_password_\"a12_45678\"_timeout__100_\"\"_";
            // String s = "password__a12345678_timeout_100";
            if (k < 0) {
                System.out.println("ERROR");
                return;
            }

            if (s == null || s.length() == 0 || s.length() > 127) {
                System.out.println("ERROR");
                return;
            }
            List<String> cmdList = new LinkedList<>();

            char[] chs = s.toCharArray();
            for (int j = 0; j < s.length(); ++j) {
                char ch = chs[j];
                String cmd = "";
                if ('"' == ch) {//如果有双引号
                    int index = s.indexOf('"', j + 1);
                    cmd = s.substring(j, index + 1);
                    j = index + 1;
                } else { //不是双引号
                    //找到下划线所在位置
                    int index = s.indexOf('_', j);
                    if (-1 != index) { // 找到了下划线
                        cmd = s.substring(j, index);
                        j = index;
                    } else {//没找到下划线
                        cmd = s.substring(j);
                        j = s.length();
                    }
                }
                if (!"".equals(cmd)) {
                    cmdList.add(cmd);
                }
            }

            if (cmdList.size() > k) {
                String s1 = cmdList.get(k);
                if (!s1.contains("\"")) {
                    cmdList.set(k, "******");
                } else {
                    cmdList.set(k, "\"******\"");
                }

                StringBuilder sb = new StringBuilder();
                cmdList.forEach(x -> sb.append(x).append("_"));
                String result = sb.substring(0, sb.length() - 1);
                System.out.print(result);
            } else {
                System.out.print("ERROR");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("ERROR");
            System.out.println("input error");
        }
    }
}
